import React from 'react';
import {connect} from 'dva';
import {Table, Row, Col, Form, Radio, Select, Button} from 'antd';
import RightConstants from '../../../utils/RightConstants';
import EditBusinessagreement from '../../../components/supplier/components/EditBusinessagreement';
import RightBox from "../../../components/commons/RightBox";
import OperatorConstant from "../../../utils/OperatorConstant";
const R = require('ramda');
const FormItem = Form.Item;
const RadioGroup = Radio.Group;



export default connect(({businessAgreement}) => ({businessAgreement}))(
    Form.create()(class businessAgreementApproval extends React.Component {

        constructor(props) {
            super(props);
        }

        componentDidMount() {
            this.props.dispatch({
                type: 'businessAgreement/getAgreementlList',
                payload: {
                    status: "2",
                    type: "approval",
                }
            });
        }


        handleResetFields = () => {
            if(this.editBusinessagreement)this.editBusinessagreement.handleResetFields();
        }



        onEdit = (record, type, isView) => {
            this.props.dispatch({type: 'businessAgreement/getAgreementById', payload: {id: record.id}});
            this.props.dispatch({type: 'businessAgreement/getAgreementApprovalList', payload: {id: record.id}});
            this.props.dispatch({type: 'businessAgreement/getCurrentNodeDetail', payload: {id: record.id, code: "agreement"}});
            this.editBusinessagreement.handleOpen(type, isView);
        }


        handleSearch = (e, flag) => {
            let values = this.props.form.getFieldsValue();
            let newPage = this.props.businessAgreement.page;
            newPage.page = flag ? newPage.page : 1;

            this.props.dispatch({
                type: 'businessAgreement/getAgreementlList',
                payload: {
                    page: newPage,
                    cstId: this.props.cstid,
                    status: values.status,
                    type: "approval"
                }
            });
        };




        columns = [
            {title: '标题', dataIndex: 'title', key: 'title'},
            {title: '正文', dataIndex: 'content', key: 'content'},
            {title: '供应商', render(record) {
                if(R.isNil(record.cstidName) || R.isEmpty(record.cstidName)){
                    return "未知";
                }else{
                    return record.cstidName;
                }
            }},
            {title: '提交人', render(record) {
                if(R.isNil(record.creatorName) || R.isEmpty(record.creatorName)){
                    return "未知";
                }else{
                    return record.creatorName;
                }

            }},
            {title: '提交时间', dataIndex: 'createtime', key: 'createtime'},
            {title: '操作', key: 'key_oprator',
                render: (text, record, index) => {

                    let  approvalBtn= record.status === "2" ?
                        <RightBox resource={RightConstants.APPROVAL.APPROVAL_AGREEMENT} split={false}>
                            <a href="#" onClick={this.onEdit.bind(this, record, "approval", false)}>审核</a>
                        </RightBox> :
                            "";



                    return (
                        <span>
                        {this.props.disabled == true || R.contains(record.status, ["1", "3", "4"])?
                            <span>
                                 <a href="#" onClick={this.onEdit.bind(this, record, "approval", true)}>查看</a>
                            </span>
                            :""
                        }

                        {approvalBtn}

                        {record.status === "5" ?
                            <span><a href="#"
                                     onClick={this.onEdit.bind(this, record, OperatorConstant.saveAndApproval, true)}>提交审核</a> </span> : ""}
                           
                    </span>
                    );
                }
            }];

        render() {
            const { getFieldDecorator } = this.props.form;
            let dataSource = R.isNil(this.props.businessAgreement.dataSource) ? {} : this.props.businessAgreement.dataSource;
            let formItemLayout = {
                labelCol: { xs: { span: 24 }, sm: { span: 6 } },
                wrapperCol: { xs: { span: 24 }, sm: { span: 14 } }
            };
            let values = this.props.form.getFieldsValue();
            let pagination = {
                showTotal: total => `共 ${total} 条数据`,
                pageSize: this.props.businessAgreement.page.size,
                total: this.props.businessAgreement.page.totalRow,
                showSizeChanger: true,
                current:this.props.businessAgreement.page.page,
                onShowSizeChange: (current, size) => {
                    let newPage = this.props.businessAgreement.page;
                    newPage.size = size;
                    newPage.page = 1;
                    this.props.dispatch({
                        type: 'businessAgreement/getAgreementlList',
                        payload: {
                            id: R.isNil(dataSource.id) ? "" : dataSource.id,
                            page: newPage,
                            cstId: this.props.cstid,
                            status: values.status,
                            type: "approval"
                        }
                    });
                },
                onChange: (pageNumber, pageSize) => {
                    let newPage = this.props.businessAgreement.page;
                    newPage.page = pageNumber;
                    this.props.dispatch({
                        type: 'businessAgreement/getAgreementlList',
                        payload: {
                            id: R.isNil(dataSource.id) ? "" : dataSource.id,
                            page: newPage,
                            cstId: this.props.cstid,
                            status: values.status,
                            type: "approval"
                        }
                    });
                },
            }

            return (
                <div>
                    <Form>
                        <Row>
                            <Col span={7}>
                                <FormItem {...formItemLayout} label={'状态'}>
                                    {getFieldDecorator('status', {
                                        initialValue: "2"
                                    })(
                                        <RadioGroup style={{width: 800}} >
                                            <Radio value="2">待审核</Radio>
                                            {/*<Radio value="3">待补充资料</Radio>*/}
                                            <Radio value="4">审核通过</Radio>
                                            <Radio value="5">审核不通过</Radio>
                                        </RadioGroup>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={3} style={{float: 'left'}}>
                                <Button type="primary" icon="search" style={{marginRight: 5}} onClick={this.handleSearch.bind(this)}>查询</Button>
                            </Col>

                        </Row>
                    </Form>
                    
                    <Table rowKey="id" dataSource={this.props.businessAgreement.list} columns={this.columns} bordered={true} size={'middle'} pagination={pagination}/>

                    <EditBusinessagreement wrappedComponentRef={(inst) => this.editBusinessagreement = inst}
                                           dispatch={this.props.dispatch}
                                           disabled={this.props.disabled}
                                           cstid={dataSource.id}
                                           handleSearch = {() => this.handleSearch(null, true)}
                                           {...this.props.businessAgreement}
                    />
                </div>
            )
        }
    })


)
